var write = {};

write.price = 1000;
write.degree = {};
write.keyword = null;
write.totalPrice = 0;
write.writeId = 0;
write.ids = ['name', 'descQues', 'descFun', 'descAche', 'descDiff', 'descBack', 'descThree'];

write.getHeight = function () {
    var height = $(document).height();
    window.parent.index.iframeResize(height);
}

write.getData = function() {
	var param = common.GetParameters();
	console.log(JSON.stringify(param));
	if (param != null && param.hasOwnProperty("type") && param.type == "update") {
		var id = param.id;
		var req = new common.Request();
		var reqData = {};
		reqData['id'] = id;
		reqData['flag'] = 1;
		var url = "orderInfo/findInfoByFlag.action";
		req.sendMethodRequest("get", url, reqData, function(e){
			console.log(e);
			if (e != null && e.isSuccess) {
				if (e.info.hasOwnProperty('degree')) {
					var degree = e.info.degree;
					if (degree != null && degree.indexOf(",")) {
						var degreeArray = degree.split(",");
						var category_info = $('.category_info');
						for (var i = 0; i < category_info.length; i ++) {
							for (var j = 0; j < degreeArray.length; j ++) {
								if (degreeArray[j] == $(category_info[i]).val()) {
									$(category_info[i]).attr('checked','checked');
									write.degree[degreeArray[j]] = degreeArray[j];
								}
							}
						}
					}
				}
				write.writeId = e.info.id;
				if (e.info.keyword != null) {
					$('#company_type').val(e.info.keyword);
				}
				$('#name').val(e.info.name);
				$('#total_price').html(e.info.price);
				$('#descQues').val(e.info.descQues);
				$('#descFun').val(e.info.descFun);
				$('#descAche').val(e.info.descAche);
				$('#descDiff').val(e.info.descDiff);
				$('#descBack').val(e.info.descBack);
				$('#descThree').val(e.info.descThree);
			}
		}, function(e){
			
		})
	}
}

write.categoryInit = function() {
	$('.category_info').click(function(){
		write.totalPrice = 0;
		var category_info = $('.category_info');
		write.degree = {};
		for (var i = 0; i < category_info.length; i ++) {
			if ($(category_info[i]).attr('checked') == "checked") {
				write.totalPrice = write.price + write.totalPrice;
				write.degree[$(category_info[i]).val()] = $(category_info[i]).val();
			}
		}
		$('#total_price').html(write.totalPrice);
	});
}

write.submit = function () {
	$('#submit_button').click(function(){
		var sendSubmit = function() {
			var param = common.GetParameters();
			var req = new common.Request();
			var reqData = {};
			var keywordStr = "";
			for (var key in write.degree) {
				keywordStr = keywordStr + key + ",";
			}
			if (keywordStr.indexOf(",") > -1) {
				keywordStr = keywordStr.substring(0, keywordStr.length - 1);
			}
			var url = "fastWriteInfo/addFastWriteInf.action";
			if (param != null && param.hasOwnProperty("type") && param.type == "update") {
				url = "fastWriteInfo/updateFastWriteInfo.action";
				reqData["id"] = write.writeId;
			}
			reqData['degree'] = keywordStr;
			reqData['keyword'] = $('#company_type').val();
			reqData['name'] = $('#name').val();
			reqData['price'] = $('#total_price').html();
			reqData['descQues'] = $('#descQues').val();
			reqData['descFun'] = $('#descFun').val();
			reqData['descAche'] = $('#descAche').val();
			reqData['descDiff'] = $('#descDiff').val();
			reqData['descBack'] = $('#descBack').val();
			reqData['descThree'] = $('#descThree').val();
			req.sendRequest(url, reqData, function(e){
				if (e != null && e.isSuccess != null && e.isSuccess) {
					window.location = "order_success.html?type=write";
				} else {
					
				}
			}, function(e){
				
			});
		}
		var ver = new verify.login(write.ids, 'empty_style', function(){
			new sendSubmit();
		}, function(){
			
		});
	});
}

write.init = function() {
	try {
		write.getHeight();
	} catch (e) {
		
	}
	write.getData();
	write.submit();
	write.categoryInit();
}

$(document).ready(function() {
	write.init();
});